/*
 * @Author: your name
 * @Date: 2021-06-04 07:58:56
 * @LastEditTime: 2022-03-17 18:53:30
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \telescope720P\src\second_page.cpp
 */
#include "second_page.h"
#include "xlog.h"
#include "common.h"
#include "interface.h"
#include "display_tips.h"
using namespace std::placeholders;
SecondPage::SecondPage()
{
    view.reset(GuiEngine::GetInstance().NewWidget());
    viewNum = 0;
    funcNum = 0;
    // setEvent[static_cast<int>(KeyboardValue::KEYBOARD_VALUE_RIGHT)] = {std::bind(&SecondPage::DefaultContrl, this, _1)};
    // setEvent[static_cast<int>(KeyboardValue::KEYBOARD_VALUE_LEFT)] = {std::bind(&SecondPage::DefaultContrl, this, _1)};
    // setEvent[static_cast<int>(KeyboardValue::KEYBOARD_VALUE_SET)] = {std::bind(&SecondPage::DefaultContrl, this, _1)};
    // setEvent[static_cast<int>(KeyboardValue::KEYBOARD_VALUE_OK)] = {std::bind(&SecondPage::DefaultContrl, this, _1)};
    // setEvent[static_cast<int>(KeyboardValue::KEYBOARD_VALUE_BACK)] = {std::bind(&SecondPage::DefaultContrl, this, _1)};
}
SecondPage::~SecondPage()
{
}
void SecondPage::ContrlSecondPage(void *ctx)
{
    IS_VALID_PTR(ctx)
    Context &data = *(Context *)ctx;
    KeyboardValue key = KeyboardValue::KEYBOARD_VALUE_END;
    data.GetData(key);
    if (key >= KeyboardValue::KEY_NUM_0 && key < KeyboardValue::KEYBOARD_VALUE_END)
    {
        if (setEvent[static_cast<int>(key)].callback)
        {
            setEvent[static_cast<int>(key)].callback(ctx);
        }
        else
        {
            LOG_E("check if the setEvent has been init.\n");
        }
    }
    else
    {
        LOG_E("contrl failed, key = %d\n", static_cast<int>(key));
    }
}
void SecondPage::SetContrlView(Widget *value)
{
    if (view.get())
    {
        view->SetWidget(value);
        LOG_I("contrl view name=%s\n", view->Name().c_str());
    }
    else
    {
        LOG_E("view is nullptr\n");
    }
}
Widget *SecondPage::GetContrlView()
{
    return view.get();
}
void SecondPage::DefaultContrl(void *ctx)
{
    LOG_W("this is a default func, will do nothing\n");
}
// void SecondPage::HandleMsg(SecondPageMsg msg)
// {
//     std::map<HandleCmd, std::function<void(void *)>>::iterator iter;
//     iter = handleEvent.find(msg.GetCmd());
//     if (iter != handleEvent.end())
//     {
//         handleEvent[msg.GetCmd()](msg.GetData());
//     }
// }
void SecondPage::RefreshPageEvent()
{
    Context *p2 = new Context;
    auto funCallback = [](void *ctx)
    {
        // std::lock_guard<std::mutex> locker(Interface::GetInstance().PageProtect());
        PageHandle *window = (PageHandle *)Interface::GetInstance().GetCurrentWin();
        IS_VALID_PTR(window)
        window->RefreshPage();
        IS_VALID_PTR(ctx)
        Context *pFunc = (Context *)ctx;
        delete pFunc;
    };
    p2->eventCallback = funCallback;
    if (!GuiEngine::GetInstance().IdleQueue(p2))
    {
        LOG_W("IdleQueue_V2 falied\n");
        delete p2;
    }
}
void SecondPage::ResetKeyCmd(KeyCmdMapTable &keyEvent)
{
    PageHandle *window = (PageHandle *)Interface::GetInstance().GetCurrentWin();
    IS_VALID_PTR(window)
    window->SetKeyEvent(keyEvent);
}
void SecondPage::RefreshPageCommon(std::string str)
{
    if (GetContrlView()->IsValid())
    {
        std::unique_ptr<Widget> view6(GuiEngine::GetInstance().NewWidget());
        if (GuiEngine::GetInstance().Lookup(GetContrlView(), "valueString", true, view6.get()))
        {
            GuiEngine::GetInstance().SetTextUtf8(view6.get(), str.c_str());
        }
    }
    else
    {
        LOG_E("invalid widget\n");
    }
}
void SecondPage::ShowSaveTips(bool ret)
{
    TipsMsg tips;
    if (ret)
    {
        tips.SetTips("save successed.\n");
    }
    else
    {
        tips.SetTips("save failed.\n");
    }
    DisplayTips::GetInstance().Display(tips);
}